import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { MdlSnackbarService, MdlDialogService } from '@angular-mdl/core';
import { TreeNode, DynamicDialogRef, DynamicDialogConfig } from 'primeng/primeng';
@Component({
    templateUrl: './rollback.component.html',
    styleUrls: ['./rollback.component.scss'],
    providers: []
})
export class RollbackComponent implements OnInit {
    systemUrl: string;
    recId: number;
    actId: number;
    submit: boolean = true;// 防止重复提交
    opinion: string = "";
    //选中案由参数
    selectedRecords: any = {};
    //退回对象
    rollbackObj: any;
    constructor(
        private http: HttpClient,
        public ref: DynamicDialogRef,
        public config: DynamicDialogConfig,
        public mdlSnackbarService: MdlSnackbarService
    ) {
        this.systemUrl = config.data.systemUrl;
        this.selectedRecords = config.data.selectedRecords;
    }

    ngOnInit() {
        this.recId = this.selectedRecords[0]['rec_id'];
        this.actId = this.selectedRecords[0]['act_id'];
        this.http.get('/' + this.systemUrl + '/api/workflow/search/rollback?recId=' + this.recId + '&actId=' + this.actId).subscribe(data => {
            this.rollbackObj = data['data'];
        });
    }

    /** 办结 */
    save() {
        if (this.rollbackObj) {
            this.submit = false;
            this.http.post('/' + this.systemUrl + '/api/workflow/rollback',
                {
                    'recId': this.selectedRecords[0]['rec_id'],
                    'actId': this.selectedRecords[0]['act_id'],
                    'transDefId': this.rollbackObj['transDefId'],
                    'rollbackActDefId': this.rollbackObj['fromActDefId'],
                    'rollbackActDefName': this.rollbackObj['fromActDefName'],
                    'rollbackPartId': this.rollbackObj['humanId'],
                    'rollbackPartName': this.rollbackObj['humanName'],
                    'opinion': this.opinion
                }).subscribe(data => {
                    this.ref.close(data);
                    this.mdlSnackbarService.showToast('回退成功！')
                });
        }
    }

    /** 取消 */
    cancel() {
        this.ref.close();
    }
}
